<?php

class TkProject
{

    private $data;
    private $locales;
    private $valid = true;
    public $errors = array();
    public $messages = array();
    public $name;

    public function __construct($data)
    {
        $this->data = $data;
        $this->name = $data['name'];
        $this->locales = $data['locales'];

        $this->checkLocales();
        $this->checkPaths();
    }

    public function isValid()
    {
        return $this->valid;
    }

    private function checkLocales()
    {
        $locales = TkApp::listSystemLocales();

        foreach ($this->locales as $locale)
        {
            if (!in_array($locale, $locales))
            {
                $this->errors[] = 'Locale <b>' . $locale . '</b> is not installed on this machine (run "<b>locale-gen ' . $locale . '</b>" in command line as root) <br />';
                $this->valid = false;
            }
        }
    }

    private function checkPaths()
    {
        if (!is_dir($this->data['project_path']))
        {
            $this->valid = false;
            $this->errors[] = "Project's folder <b>'" . $this->data['project_path'] . "'</b> doesn't exists";
        }
        elseif (!is_readable($this->data['project_path']))
        {
            $this->valid = false;
            $this->errors[] = "Project's folder <b>'" . $this->data['project_path'] . "'</b> is not readable, plese set correct rights";
        }

        if (!is_dir($this->data['locales_path']))
        {
            $this->valid = false;
            $this->errors[] = "Locales folder <b>'" . $this->data['locales_path'] . "'</b> doesn't exists";
        }
        elseif (!is_writable($this->data['locales_path']))
        {
            $this->valid = false;
            $this->errors[] = "Locales folder <b>'" . $this->data['locales_path'] . "'</b> is not writable, plese set correct rights";
        }
    }

    public function getErrors()
    {
        
    }

    private function parse()
    {
        $types = explode(",", $this->data['file_types']);

        foreach ($types as $key => $type)
        {
            $types[$key] = '"*.' . trim($type) . '"';
        }

        $types = implode(" -o -iname ", $types);
        $types = "-iname " . $types;

        $methods = explode(",", $this->data['methods']);
        $methods = implode(" --keyword=", $methods);
        $methods = " --keyword=" . $methods;

        $folder = $this->data['project_path'];
        $locales_path = $this->data['locales_path'];
        $encoding = $this->data['sources_encoding'];

        $command = 'find ' . $folder . ' . \( ' . $types . ' \) | xargs xgettext --from-code ' . $encoding . ' ' . $methods . ' -L php -o ' . $locales_path . 'messages.pot ';


        shell_exec($command);
    }

    private function createFolder($folder)
    {
        if (!is_dir($folder))
        {
            mkdir($folder, 0777);
        }
    }

    public function synchronize()
    {
        $this->parse();

        $message = '';
        $locales_path = $this->data['locales_path'];

        $tk_client = new TKClient($this->data['project_id'], $this->data['api_secret']);

        $response = $tk_client->pushPotFile(file_get_contents($locales_path . 'messages.pot'));

	if ($response->status == 'OK')
        {
            $this->messages[] = $response->data->text.'<br />';
        }

        foreach ($this->locales as $locale)
        {
            $path = $locales_path . $locale;
            $this->createFolder($path);

            $path .= '/LC_MESSAGES';
            $this->createFolder($path);

            $response = $tk_client->getMoFile($locale);
            $json = json_decode($response);

            if (!empty($json))
            {
                $this->errors[] = $json->error . '<br />';
            }
            else
            {
                $path .= '/messages.mo';

                $handle = fopen($path, 'w');
                fwrite($handle, $response);
                fclose($handle);
            }
        }

        $this->messages[] = 'Project <b>' . $this->name . '</b> was synchronized with TranslateKarate.com';
    }

    private function generateMo()
    {
        foreach ($this->locales as $locale)
        {
            $po_file = $locales_path . 'messages_' . $locale . '.po';
            $command = 'msginit -l ' . $locale . ' -o ' . $po_file . ' -i ' . $locales_path . 'messages.pot';
            $message .= $command . '<hr />';
            shell_exec($command);

            $scommand = 'msgfmt ' . $po_file . ' -o ' . $locales_path . $locale . '/LC_MESSAGES/messages.mo';
            $message .= $command . '<hr />';
            shell_exec($command);
        }
    }
}